// pages/addFood/addFood.js
const utils = require('../../utils/util');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    department: [],
    dIndex: '',
    department_id: '',
    bookType: [
      {
        type: 1,
        label: '常规餐'
      },
      {
        type: 2,
        label: '清真餐'
      },
      {
        type: 3,
        label: '全素餐'
      }
    ],
    bIndex: 0,
    type: 1,
    riceType: [
      {
        rice_type: 1,
        label: '需要'
      },
      {
        rice_type: 2,
        label: '不需要'
      }
    ],
    rice_type: 1,
    rIndex: 0,
    date_type: ''
  },
  submitFood:function(e){
    const that = this;
      // util.postAjax(app.globalData.apiUrl +'/Api/Food/yes_food',function(res){
    if(!that.data.department_id) {
      utils.showToast('请选择部门！', 'none', 1000)
      return
    } else if(!that.data.num) {
      utils.showToast('请填写份数！', 'none', 1000)
      return
    } 
    utils.showModelAll('提示', 
    `该操作将为部门 【${that.data.department[that.data.dIndex].content_name}】 加餐 【${that.data.num}】 份，餐别为： 【 ${that.data.bookType[that.data.bIndex].label}-${that.data.riceType[that.data.rIndex].label}主食 】，是否确认？`, ()=> {
      console.log('提交')
      let data = {
        date: that.data.date,
        type: that.data.type,
        rice_type: that.data.rice_type,
        content_id: that.data.department_id,
        date_type: that.data.dateType,
        add_type: 1
      }
      data = JSON.stringify(data)
      console.log(data)
      let j = 0;
      for(let i=0; i<that.data.num; i++) {
        utils.postAjax(app.globalData.apiUrl +'/Api/Food/yes_food', res => {
          console.log(res.data)
          if(res.data == '1') {
             j = j + 1
            //  console.log(j)
             if(j == that.data.num) {
               utils.showToast('加餐成功！', 'none', 2000)
               that.setData({
                 dIndex: '',
                 department_id: '',
                 num: '',
                 rIndex: 0,
                 rice_type: 1,
                 bIndex: 0,
                 type: 1
               })
             }
          }
        },{
          date: that.data.date,
          pro: data
        })
      }
    })
  },
  inputNum(e){
    console.log(e)
    this.setData({
      num: e.detail.value
    })
  },
   // 获取部门
  getDepartment() {
    let that = this;
    utils.getAjax(`${app.globalData.apiUrl}/Api/Food/get_food_content`,res => {
      const department = that.data.department.concat(res.data)
      // console.log(department)
      that.setData({
        department: department
      })
      // console.log(that.data.department)
    },{
        status: 1
    })
  },
  departmentChange(e) {
    // console.log(e)
    let index = e.detail.value;
    let that = this;
    this.setData({
      department_id: that.data.department[index].id,
      dIndex: index
    })
  },
  bookTypeChange(e) {
    let index = e.detail.value;
    let that = this;
    this.setData({
      type: that.data.bookType[index].type,
      bIndex: index
    })
  },
  riceTypeChange(e) {
    let index = e.detail.value;
    let that = this;
    this.setData({
      rice_type: that.data.bookType[index].type,
      rIndex: index
    })
  },
  setDateType() {
    let timeArr = app.globalData.mainTime;
    let am = utils.setDate('00:00', timeArr[1].food_time); 
    let pm = utils.setDate(timeArr[1].food_time, '23:59');
    let dateType = '';
    if(am) {
      dateType = 1
    } else if(pm) {
      dateType = 2
    }
    this.setData({
      dateType: dateType
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    utils.setNavBarTitle('临时加餐')
    const date = utils.dateFormat()
    this.setData({
      date: date
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDepartment()
    this.setDateType()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})